package cq.zhcqk.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.app;
import cq.zhcqk.util.AsyncHttpClientUtils;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.SpUtil;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/11/21.
 */
public class MineActivity extends Activity implements View.OnClickListener{
    private TextView mine_register;
    private TextView phonet;
    private AsyncHttpClient httpClient;
    private Button login_btn;
    private SharedPreferences mSharedPreferences;
    private Context context;

    private RoundedImageView head_img;

    private LinearLayout lay1;
    private LinearLayout lay2;

    private LinearLayout mine_sc;
    private LinearLayout mine_fk;
    private LinearLayout mine_sz;
    private LinearLayout mine_diancan;
    private LinearLayout mine_xx;
    private LinearLayout mine_ab;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        context = this;
        httpClient = AsyncHttpClientUtils.getInstance().getAsyncHttpClient();
        mSharedPreferences = SpUtil.getSpBykey(context, SpUtil.TOKEN);
        initView();
        initBack();
    }

    private void initView(){
        mine_register = (TextView) findViewById(R.id.mine_register);
        phonet = (TextView) findViewById(R.id.phone);
        login_btn = (Button) findViewById(R.id.login_btn);
        head_img = (RoundedImageView) findViewById(R.id.head_img);
        lay1 = (LinearLayout) findViewById(R.id.lay1);
        lay2 = (LinearLayout) findViewById(R.id.lay2);

        mine_sc = (LinearLayout) findViewById(R.id.mine_sc);
        mine_fk = (LinearLayout) findViewById(R.id.mine_fk);
        mine_sz = (LinearLayout) findViewById(R.id.mine_sz);
        mine_xx = (LinearLayout) findViewById(R.id.mine_xx);
        mine_diancan = (LinearLayout) findViewById(R.id.mine_diancan);
        mine_ab = (LinearLayout) findViewById(R.id.mine_ab);

        mine_register.setOnClickListener(this);
        login_btn.setOnClickListener(this);

        mine_sc.setOnClickListener(this);
        mine_diancan.setOnClickListener(this);
        mine_fk.setOnClickListener(this);
        mine_sz.setOnClickListener(this);
        mine_xx.setOnClickListener(this);
        mine_ab.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String phone = mSharedPreferences.getString("phone",null);
        String headImg = mSharedPreferences.getString("picaddress",null);
        String nick = mSharedPreferences.getString("var01",null);
        if(!app.isLogin){
            lay1.setVisibility(View.VISIBLE);
            lay2.setVisibility(View.GONE);
        }
        else if (phone != null && !"".equals(phone)){
            if(headImg != null){
                ImageLoader.getInstance().displayImage(Common.URL+headImg , head_img, ImageLoaderHelper.getOptionsHead());
            }
            if(nick != null){
                phonet.setText(nick);
            }else{
                phonet.setText(phone);
            }
            lay2.setVisibility(View.VISIBLE);
            lay1.setVisibility(View.GONE);
        }else {
            lay1.setVisibility(View.VISIBLE);
            lay2.setVisibility(View.GONE);
        }
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("个人中心");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.mine_register:
//                startActivity(new Intent(this,RegisterActivity.class));
                startActivity(new Intent(this,LoginActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.login_btn:
                startActivity(new Intent(this,LoginActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;

            case R.id.mine_sc:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(this,MineShouChangActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.mine_fk:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(this,MineFankuiActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.mine_sz:
                startActivity(new Intent(this,MineSettingActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.mine_xx:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(this,MinexxActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.mine_diancan:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(this,AXST_OrderListActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.mine_ab:
                startActivity(new Intent(this,MineAboutActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
        }
    }
}
